<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.StringWriter"%>
<%@page import="java.io.Writer"%>
<%@page import="org.apache.http.params.CoreConnectionPNames"%>
<%@page import="org.apache.http.NameValuePair"%>
<%@page import="org.apache.http.client.entity.UrlEncodedFormEntity"%>
<%@page import="org.apache.http.message.BasicNameValuePair"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.net.URI"%>
<%@page import="org.apache.http.ParseException"%>
<%@page import="org.apache.http.util.EntityUtils"%>
<%@page import="org.apache.http.HttpEntity"%>
<%@page import="org.apache.http.client.methods.HttpUriRequest"%>
<%@page import="org.apache.http.HttpResponse"%>
<%@page import="org.apache.http.entity.StringEntity"%>
<%@page import="org.apache.http.client.methods.HttpPost"%>
<%@page import="java.io.IOException"%>
<%@page import="org.apache.http.client.ClientProtocolException"%>
<%@page import="java.net.URISyntaxException"%>
<%@page import="org.apache.http.impl.client.DefaultHttpClient"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%!
/**
 * 发送post请求
 * @param url
 * @param charset
 * @param headerParams
 * @param formParams
 * @param bodyParam
 * @param bodyContentType
 * @param readTimeOut
 * @param connTimeOut
 * @return
 * @throws URISyntaxException
 * @throws ClientProtocolException
 * @throws IOException
 */
public String post(String url, String charset, Map<String, String> headerParams,Map<String, String> formParams, String bodyParam,String bodyContentType,int readTimeOut,int connTimeOut) throws URISyntaxException, ClientProtocolException, IOException {  
    DefaultHttpClient httpclient = new DefaultHttpClient(); 
    httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeOut * 1000); 
    httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connTimeOut * 1000); 
    String body = null;  
    HttpPost post = postForm( url, formParams, charset);  
    //post.setHeader("Content-Type", "application/json");
    if( headerParams!=null ){
    	for(Map.Entry<String, String> entry : headerParams.entrySet()){
    		post.setHeader(entry.getKey(), entry.getValue());
    	}
    }
    if( bodyParam!=null && !"".equals(bodyParam) ){
    	StringEntity entity = new StringEntity(bodyParam,charset);//解决中文乱码问题    
    	//entity.setContentType("application/json");  
    	entity.setContentType(bodyContentType);
    	post.setEntity(entity);
    }
    body = invoke(httpclient, post);  
    httpclient.getConnectionManager().shutdown();  
    return body;  
}
      
/**
 * 执行
 * @param httpclient
 * @param httpost
 * @return
 * @throws ClientProtocolException
 * @throws IOException
 */
private String invoke(DefaultHttpClient httpclient, HttpUriRequest httpost) throws ClientProtocolException, IOException {  
    HttpResponse response = sendRequest(httpclient, httpost);  
    String body = paseResponse(response);  
    return body;  
}  

/**
 * 获取请求结果
 * @param response
 * @return
 * @throws ParseException
 * @throws IOException
 */
private static String paseResponse(HttpResponse response) throws ParseException, IOException {  
    HttpEntity entity = response.getEntity();  
    String body = EntityUtils.toString(entity,"utf-8");  
    return body;  
}  

/**
 * 发送请求
 * @param httpclient
 * @param httpost
 * @return
 * @throws ClientProtocolException
 * @throws IOException
 */
private HttpResponse sendRequest(DefaultHttpClient httpclient, HttpUriRequest httpost) throws ClientProtocolException, IOException {  
    HttpResponse response = httpclient.execute(httpost);  
    return response;  
}  

/**
 * 创建HttpPost对象
 * @param url	
 * @param params	
 * @param charset	
 * @return
 * @throws URISyntaxException
 * @throws UnsupportedEncodingException
 */
private HttpPost postForm(String url, Map<String, String> params,String charset) throws URISyntaxException, UnsupportedEncodingException{  
	URI uri = new URI(url);
	HttpPost httpost = new HttpPost(uri);  
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
    if(params!=null){
        for(String key : params.keySet()) {  
            nvps.add(new BasicNameValuePair(key, params.get(key)));  
        }  
    }
   httpost.setEntity(new UrlEncodedFormEntity(nvps, charset));  
   return httpost;  
} 


%>
